package dbmanager.connectionManager;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

import dbmanager.struct.ConnectionFolder;
import dbmanager.struct.DBConnection;

public class TreeModel extends DefaultTreeModel {

	public TreeModel(TreeNode name) {
		super(name);
	}

	@Override
	public boolean isLeaf(Object arg0) {
		if (((DefaultMutableTreeNode) arg0).getUserObject() instanceof DBConnection) {
			return true;
		}
		return false;
		// return true;
	}

	public void addFolder(ConnectionFolder newFolder, DefaultMutableTreeNode parentFolder) {
		DefaultMutableTreeNode newConnectionNode = new DefaultMutableTreeNode();
		newConnectionNode.setUserObject(newFolder);
		insertNodeInto(newConnectionNode, parentFolder, parentFolder.getChildCount());
	}

	public void addFolder(ConnectionFolder newFolder) {
		DefaultMutableTreeNode root = (DefaultMutableTreeNode) getRoot();
		addFolder(newFolder,root);
	}

	public void addConncetion(DBConnection newCon, DefaultMutableTreeNode parentFolder) {
		DefaultMutableTreeNode newConnectionNode = new DefaultMutableTreeNode();
		newConnectionNode.setUserObject(newCon);
		insertNodeInto(newConnectionNode, parentFolder, parentFolder.getChildCount());
	}

	public void addConncetion(DBConnection newCon) {
		DefaultMutableTreeNode root = (DefaultMutableTreeNode) getRoot();
		addConncetion(newCon,root);
	}
}
